package com.cata.mall.portal.controller;

import com.cata.mall.common.api.CommonResult;
import com.cata.mall.model.UmsMember;
import com.cata.mall.portal.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * @作者: Tao
 * @日期: 2025/10/20
 * @时间: 10:53
 * @描述: 会员管理Controller<pre>
 */
@RestController
@Api(tags = "UmsMemberController")
@Tag(name = "UmsMemberController", description = "会员管理")
@RequestMapping("/sso")
public class UmsMemberController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private UmsMemberService memberService;

    /**
     * 会员注册接口
     *
     * @param username  用户名
     * @param password  密码
     * @param telephone 手机号码
     * @param authCode  验证码
     * @return 注册结果，成功时返回"注册成功"提示信息
     */
    @ApiOperation("会员注册")
    @PostMapping(value = "/register")
    public CommonResult register(@RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String telephone,
                                 @RequestParam String authCode) {
        memberService.register(username, password, telephone, authCode);
        return CommonResult.success(null, "注册成功");
    }

    /**
     * 会员登录接口
     *
     * @param username 用户名
     * @param password 密码
     * @return 登录成功返回token信息，失败返回错误提示
     */
    @ApiOperation("会员登录")
    @PostMapping(value = "/login")
    public CommonResult login(@RequestParam String username,
                              @RequestParam String password) {
        // 执行用户登录验证
        String token = memberService.login(username, password);
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        // 构造返回的token信息
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }


    /**
     * 获取当前登录会员的信息
     *
     * @param principal 当前认证用户信息
     * @return 会员信息结果，未授权时返回unauthorized，成功时返回会员信息
     */
    @ApiOperation("获取会员信息")
    @GetMapping(value = "/info")
    public CommonResult info(Principal principal) {
        // 检查用户是否已认证
        if (principal == null) {
            return CommonResult.unauthorized(null);
        }
        // 获取当前登录会员信息并返回
        UmsMember member = memberService.getCurrentMember();
        return CommonResult.success(member);
    }

    /**
     * 获取验证码
     *
     * @param telephone 手机号码
     * @return 包含验证码的通用返回结果
     */
    @ApiOperation("获取验证码")
    @GetMapping(value = "/getAuthCode")
    public CommonResult getAuthCode(@RequestParam String telephone) {
        String authCode = memberService.generateAuthCode(telephone);
        return CommonResult.success(authCode, "获取验证码成功");
    }

    /**
     * 会员修改密码接口
     *
     * @param telephone 会员手机号码
     * @param password  新密码
     * @param authCode  验证码
     * @return 通用返回结果，密码修改成功时返回成功信息
     */
    @ApiOperation("会员修改密码")
    @PostMapping(value = "/updatePassword")
    public CommonResult updatePassword(@RequestParam String telephone,
                                       @RequestParam String password,
                                       @RequestParam String authCode) {
        memberService.updatePassword(telephone, password, authCode);
        return CommonResult.success(null, "密码修改成功");
    }

    @ApiOperation(value = "刷新token")
    @GetMapping(value = "/refreshToken")
    public CommonResult refreshToken(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        String refreshToken = memberService.refreshToken(token);
        if (refreshToken == null) {
            return CommonResult.failed("token已经过期！");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", refreshToken);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }
}
